package com.atguigu.flink.chapter10;

import jdk.nashorn.internal.ir.EmptyNode;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class JdbcDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env.enableCheckpointing(3000);

        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.executeSql("create table sensor(" +
                "id int," +
                "ts bigint," +
                "vc int" +
                ") with(" +
                " 'connector' = 'kafka', " +
                " 'topic' = 's1', " +
                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
                " 'properties.group.id' = 'atguigu', " +
                " 'scan.startup.mode' = 'latest-offset'," +
                " 'format' = 'csv' " +
                ")");

        Table table = tEnv.sqlQuery("select * from sensor");

        tEnv.executeSql("create table mysql_table(" +
                "id int," +
                "ts bigint," +
                "vc int," +
                "primary key(id) not enforced" +
                ") with(" +
                " 'connector' = 'jdbc', " +
                " 'url' = 'jdbc:mysql://hadoop102:3306/flink?useSSL=false', " +
                " 'table-name' = 'ws2', " +
                //" 'sink.buffer-flush.max-rows' = '500', " +
                //" 'sink.buffer-flush.interval' = '5s', " +
                " 'username' = 'root', " +
                " 'password' = '000000' " +
                ")");
        table.executeInsert("mysql_table");
    }
}


//public class JdbcDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        env.enableCheckpointing(3000);
//
//        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
//
//        tEnv.executeSql("create table sensor(" +
//                "id int," +
//                "ts bigint," +
//                "vc int" +
//                ") with(" +
//                " 'connector' = 'kafka', " +
//                " 'topic' = 's1', " +
//                " 'properties.bootstrap.servers' = 'hadoop102:9092', " +
//                " 'properties.group.id' = 'atguigu', " +
//                " 'scan.startup.mode' = 'latest-offset'," +
//                " 'format' = 'csv' " +
//                ")");
//
//
//        Table table = tEnv.sqlQuery("select * from sensor");
//
//        tEnv.executeSql("create table mysql_table(" +
//                "id int," +
//                "ts bigint," +
//                "vc int," +
//                "primary key(id) not enforced" +
//                ") with(" +
//                " 'connector' = 'jdbc', " +
//                " 'url' = 'jdbc:mysql://hadoop102:3306/flink?useSSL=false', " +
//                " 'table-name' = 'ws2', " +
//                //" 'sink.buffer-flush.max-rows' = '500', " +
//                //" 'sink.buffer-flush.interval' = '5s', " +
//                " 'username' = 'root', " +
//                " 'password' = '000000' " +
//                ")");
//
//        table.executeInsert("mysql_table");
//    }
//}